package com.muli.muwai.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfOrder;
import com.muli.muwai.exception.MyDefinedEnum;
import com.muli.muwai.mapper.OrderMapper;
import com.muli.muwai.service.OrserSercive;
import com.muli.muwai.util.DateUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrserSerciveImpl extends ServiceImpl<OrderMapper, IntfOrder> implements OrserSercive {

    @Autowired
    OrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderExcel(List<String[]> orderExcel) throws Exception {
        //将List<String[]>转化成List<IntfOrder>
        List<IntfOrder> orders = getIntfOrders(orderExcel);

        //以下代码为正确数据代码 测试数据请注释
        HashSet<Integer> set = new HashSet<>();
        for (IntfOrder order : orders) {
            //获取时间
            Date startDate = order.getStartDate();
            //获取Calendar对象
            Calendar calendar = Calendar.getInstance();
            //将startDate转化成Calendar对象
            calendar.setTime(startDate);
            //取出月份，要加1，因为是从0开始的(!)不知道为什么Sun要这样?
            int i = calendar.get(Calendar.MONTH) + 1;
            set.add(i);
        }
        //如果月份一致则里面只有一个数据,执行删除,否侧报错
        if (set.size() == 1) {
            //删除同月份的数据
            String strip = StringUtils.strip(set.toString(), "[]");
            int anInt = Integer.parseInt(strip);
            int aa = orderMapper.deleteData(anInt);
            saveBatch(orders);
            System.out.println(aa);
        } else {
            throw new Exception(MyDefinedEnum.ERROR_MONTH.getMsg());
        }

        //测试数据
        //saveBatch(orders);
    }

    private List<IntfOrder> getIntfOrders(List<String[]> orderExcel) throws Exception {
        List<IntfOrder> orders = new ArrayList<>();
        for (String[] strings : orderExcel) {
            IntfOrder intfOrder = new IntfOrder();
            intfOrder.setFiscalYear(strings[0]);
            intfOrder.setProfit(strings[1]);
            //String转data
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
                Date date1 = sdf.parse(strings[2]);
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                intfOrder.setStartDate(timestamp1);
            } catch (Exception e) {
                int size = orders.size() + 2;
                throw new Exception("第" + size + "行,第" + 3 + "列,请设写成yyyy/m/d格式");
            }
            intfOrder.setPartCompany(strings[3]);
            intfOrder.setShowRetail(strings[4]);
            intfOrder.setOrderNumber(strings[5]);
            intfOrder.setProjectName(strings[6]);
            // intfOrder.setStartAmount(strings[7]);
            try {
                if (strings[7].equals("")) {
                    intfOrder.setStartAmount(Double.valueOf(0));
                } else {
                    intfOrder.setStartAmount(Double.parseDouble(strings[7]));
                }
            } catch (Exception e) {
                int size = orders.size() + 2;
                throw new Exception("第" + size + "行,第" + 8 + "列,格式错误");
            }
            orders.add(intfOrder);
        }
        return orders;
    }
}
